package com.lxmes.flowable.controller;

import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.core.page.TableDataInfo;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.system.domain.SysSpTask;
import com.lxmes.system.service.ISysSpTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 审批任务Controller
 * 
 * @author zhangzhiyu
 * @date 2023-06-28
 */
@Api(tags = "审批任务")    
@RestController
@RequestMapping("/system/spTask")
public class SysSpTaskController extends BaseController
{
    @Autowired
    private ISysSpTaskService sysSpTaskService;

    /**
     * 查询审批任务列表
     */
    @ApiOperation("查询审批任务列表")
    @PreAuthorize("@ss.hasPermi('system:spTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSpTask sysSpTask)
    {
        startPage();
        List<SysSpTask> list = sysSpTaskService.selectSysSpTaskList(sysSpTask);
        return getDataTable(list);
    }

    /**
     * 导出审批任务列表
     */
    @ApiOperation("导出审批任务列表")
    @PreAuthorize("@ss.hasPermi('system:spTask:export')")
    @Log(title = "审批任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSpTask sysSpTask)
    {
        List<SysSpTask> list = sysSpTaskService.selectSysSpTaskList(sysSpTask);
        ExcelUtil<SysSpTask> util = new ExcelUtil<SysSpTask>(SysSpTask.class);
        util.exportExcel(response, list, "审批任务数据");
    }

    /**
     * 获取审批任务详细信息
     */
    @ApiOperation("获取审批任务详细信息")
    @PreAuthorize("@ss.hasPermi('system:spTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysSpTaskService.selectSysSpTaskById(id));
    }

    /**
     * 新增审批任务
     */
    @ApiOperation("新增审批任务")
    @PreAuthorize("@ss.hasPermi('system:spTask:add')")
    @Log(title = "审批任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSpTask sysSpTask)
    {
        return toAjax(sysSpTaskService.insertSysSpTask(sysSpTask));
    }
    @PostMapping("/spSubTask")
    public AjaxResult spSubTask(@RequestBody SysSpTask sysSpTask)
    {
        return toAjax(sysSpTaskService.spSubTask(sysSpTask));
    }


    /**
     * 修改审批任务
     */
    @ApiOperation("修改审批任务")
    @PreAuthorize("@ss.hasPermi('system:spTask:edit')")
    @Log(title = "审批任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSpTask sysSpTask)
    {
        return toAjax(sysSpTaskService.updateSysSpTask(sysSpTask));
    }

    /**
     * 删除审批任务
     */
    @ApiOperation("删除审批任务")
    @PreAuthorize("@ss.hasPermi('system:spTask:remove')")
    @Log(title = "审批任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysSpTaskService.deleteSysSpTaskByIds(ids));
    }
    /**
     * 导入审批任务
     */
    @Log(title = "审批任务", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysSpTask> util = new ExcelUtil<>(SysSpTask.class);
        List<SysSpTask> sysSpTaskList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = sysSpTaskService.imporIteminfo(sysSpTaskList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<SysSpTask> util = new ExcelUtil<>(SysSpTask.class);
        util.importTemplateExcel(response,"审批任务");
    }
}
